package per.turtle.system.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import per.turtle.system.entity.SysUser;
import per.turtle.system.service.ISysMenuService;
import per.turtle.system.service.ISysPermissionService;
import per.turtle.system.service.ISysRoleService;

import java.util.HashSet;
import java.util.Set;

/**
 * 权限 服务层实现
 *
 * @author turtle
 */
@Service
public class SysPermissionServiceImpl implements ISysPermissionService {

    @Autowired
    private ISysRoleService roleService;

    @Autowired
    private ISysMenuService menuService;

    @Override
    public Set<String> getMenuPermission(String userId) {
        Set<String> perms = new HashSet<>();
        // 管理员拥有所有权限
        if (SysUser.isAdmin(userId)) {
            perms.add("*:*:*");
        } else {
            perms.addAll(menuService.selectMenuPermsByUserId(userId));
        }
        return perms;
    }

    @Override
    public Set<String> getRolePermission(String userId) {
        Set<String> roles = new HashSet<>();
        // 管理员拥有所有权限
        if (SysUser.isAdmin(userId)) {
            roles.add("admin");
        } else {
            roles.addAll(roleService.selectRolePermissionByUserId(userId));
        }
        return roles;
    }

}
